#!/usr/local/bin/python

import os
import time
import sqlite3

dbExist = os.path.exists('users.db')

dbConn = sqlite3.connect('users.db')
cursor = dbConn.cursor()

if not dbExist:
	cursor.execute("CREATE TABLE users( email VARCHAR(64))")
	cursor.execute("CREATE INDEX idx_users ON users( email)")

progress = 0
added = 0
exist = 0

for fileName in os.listdir('input'):
	if fileName.endswith('.txt'):
		filePath = os.path.join('input', fileName)
		for line in open(filePath).readlines():
			progress += 1
			email = line.strip()
			cursor.execute("SELECT email FROM users WHERE email='%s'" % email)
			if cursor.fetchone():
				print '%d - %s - exists' % (progress, email)
				exist += 1
			else:
				cursor.execute("INSERT INTO users VALUES('%s')" % email)
				print '%d - %s - added' % (progress, email)
				added += 1
		os.remove(filePath)
dbConn.commit()

print '\nSUMMARY'
print '%d already exist' % (exist)
print '%d new added' % (added)

outFile = open('output/users_%s.txt' % time.strftime('%Y%m%d_%H%M'), 'wt')
cursor.execute("SELECT email from users ORDER BY email")

for row in cursor.fetchall():
	outFile.write('%s\n' % row[0])

outFile.close()
dbConn.close()
